package thread;

/**
 * 测试volatile光健字
 */
public class VolatileTest {
    public boolean cancel = false;

    public class MyRunnable implements Runnable {

        @Override
        public void run() {
            while (!cancel) {
                System.out.println(Thread.currentThread().getName() + ":线程运行");
            }
        }
    }

    public void running() {
        new Thread(new MyRunnable(), "TestThread").start();
        try {
            Thread.sleep(1000);
            cancel = true;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new VolatileTest().running();
    }
}
